﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using System.Web.Security;

namespace MShop.Core.Service.Membership
{
    public class DefaultUserRightsService:IUserRightsService
    {
        private readonly ICommonDao<aspnet_Users> _UsersDao;
        private readonly IMenuDao _MenuDao;
        private readonly ICommonDao<ShopUserRights> _UserRightDao;
        private readonly IShopContext _ShopContext;
        private readonly ICommonDao<MenuPage> _MenuPageDao;
        public DefaultUserRightsService(ICommonDao<aspnet_Users> usersDao , IMenuDao menuDao, ICommonDao<ShopUserRights> userRightDao,
            IShopContext shopContext,ICommonDao<MenuPage> menuPageDao)
        {
            _UsersDao = usersDao;
            _MenuDao = menuDao;
            _UserRightDao = userRightDao;
            _ShopContext = shopContext;
            _MenuPageDao = menuPageDao;
        }


     

        public IEnumerable<Domain.ShopUserRights> CurrentUserRights
        {
            get 
            {
               return _ShopContext.CurrentUser.ShopUserRights;
            }
        }

        public Domain.ShopUserRights CurrentUserRight(int menuId)
        {
            var r = CurrentUserRights.FirstOrDefault(x => x.MenuId == menuId && x.UserId == _ShopContext.CurrentUser.UserId);
            return r;
        }

        public RightFlags CurrentUserRightFlags(int menuId)
        {
            var r = CurrentUserRights.FirstOrDefault(x => x.MenuId == menuId && x.UserId == _ShopContext.CurrentUser.UserId);
            if (r != null)
                return r.RightFlags;
            return RightFlags.NoRight;
        }

        public Domain.RightFlags CurrentUserRightFlags(string urlReferrer)
        {
            if (urlReferrer.LastIndexOf('/') == 1)
            {
                urlReferrer = urlReferrer.Substring(0, urlReferrer.Length - 1);
            }
            Menu menu = _MenuDao.FirstOrDefault(x => x.Url.ToLower() == urlReferrer);

            if (menu != null)
            {
               
                ShopUserRights right = _UserRightDao.GetAll().SingleOrDefault(x => x.MenuId == menu.Id &&
                    x.UserId == _ShopContext.CurrentUser.UserId);
                if (right != null)
                {
                    return (RightFlags)right.Rights;
                }
            }
            else
            {
                MenuPage menupage = _MenuPageDao.FirstOrDefault(x => x.Url.ToLower() == urlReferrer);
                if (menupage != null)
                {
                  
                    ShopUserRights right = _UserRightDao.GetAll().SingleOrDefault(x => x.MenuId == menupage.MenuId &&
                    x.UserId == _ShopContext.CurrentUser.UserId);
                    if (right != null)
                    {
                        return (RightFlags)right.Rights;
                    }
                }


            }
            return RightFlags.NoRight;
        }


        public RightFlags CurrentUserRightFlags(System.Web.Routing.RouteData routedata)
        {

            string requestUrl = string.Empty;
            string area = routedata.DataTokens["area"] == null ? string.Empty : routedata.DataTokens["area"].ToString();
            string actionName = routedata.Values["action"].ToString();
            string controllerName = routedata.Values["controller"].ToString();
            if (area == string.Empty)
            {
                requestUrl = string.Concat("/", controllerName.ToLower(), "/", actionName.ToLower());
            }
            else
            {
                requestUrl = string.Concat("/", area, "/", controllerName.ToLower(), "/", actionName.ToLower());
            }
            return this.CurrentUserRightFlags(requestUrl);

        }

        public string CurrentRole
        {
            get { return Roles.GetRolesForUser(_ShopContext.CurrentUser.UserName)[0]; }
        }


        
    }
}